package com.zbkj.crmeb.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.BeanMapper;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.DateUtil;
import com.zbkj.crmeb.user.dao.UserMonthBuyDao;
import com.zbkj.crmeb.user.model.UserInviteReward;
import com.zbkj.crmeb.user.model.UserMonthBuy;
import com.zbkj.crmeb.user.request.UserMonthBuyRequest;
import com.zbkj.crmeb.user.service.UserInviteRewardService;
import com.zbkj.crmeb.user.service.UserMonthBuyService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.zbkj.crmeb.user.vo.UserMonthBuyVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Slf4j
@Service
public class UserMonthBuyServiceImpl extends ServiceImpl<UserMonthBuyDao, UserMonthBuy> implements UserMonthBuyService {

    @Resource
    private UserMonthBuyDao dao;
    @Resource
    private UserInviteRewardService userInviteRewardService;

    @Override
    public UserMonthBuy getByUserIdDate(Integer userId, String curDate) {
        LambdaQueryWrapper<UserMonthBuy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserMonthBuy::getUserId, userId);
        wrapper.eq(UserMonthBuy::getCurDate, curDate);
        return dao.selectOne(wrapper);
    }

    @Override
    public PageInfo<UserMonthBuyVO> getList(UserMonthBuyRequest request, PageParamRequest pageParamRequest) {
        Page<UserMonthBuy> pageUser = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<UserMonthBuyVO> datas = BeanMapper.mapList(findByList(request), UserMonthBuyVO.class);
        fullReward(datas);
        return CommonPage.copyPageInfo(pageUser, datas);
    }

    @Override
    public List<UserMonthBuyVO> findAll(UserMonthBuyRequest request) {
        List<UserMonthBuyVO> datas = BeanMapper.mapList(findByList(request), UserMonthBuyVO.class);
        fullReward(datas);
        return datas;
    }

    private void fullReward(List<UserMonthBuyVO> datas) {
        if (CollectionUtils.isEmpty(datas)) {
            return;
        }
        List<Integer> monthIds = datas.stream().map(UserMonthBuy::getId).collect(Collectors.toList());
        Map<Integer, List<UserInviteReward>> rewardMap = userInviteRewardService.findByMonthIds(monthIds).stream().collect(Collectors.groupingBy(UserInviteReward::getMonthId));
        if (rewardMap.isEmpty()) {
            return;
        }
        datas.forEach(data -> {
            if (rewardMap.containsKey(data.getId())) {
                List<UserInviteReward> rewards = rewardMap.get(data.getId());
                // 推荐金额累加
                BigDecimal inviteReward = rewards.stream().filter(f -> f.getRewardType() == 0).map(UserInviteReward::getReward).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal againReward = rewards.stream().filter(f -> f.getRewardType() == 1).map(UserInviteReward::getReward).reduce(BigDecimal.ZERO, BigDecimal::add);
                data.setInviteReward(inviteReward);
                // 复购金额累加
                data.setAgainReward(againReward);
            }
        });
    }

    private List<UserMonthBuy> findByList(UserMonthBuyRequest request) {
        LambdaQueryWrapper<UserMonthBuy> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(request.getKeys())) {
            wrapper.and(i -> i
                    .or().like(UserMonthBuy::getUserName, request.getKeys())
                    .or().eq(UserMonthBuy::getUserId, request.getKeys()));
        }
        if (StringUtils.isNotBlank(request.getCurDate())) {
            wrapper.eq(UserMonthBuy::getCurDate, request.getCurDate());
        }
        wrapper.orderByDesc(UserMonthBuy::getCurDate);
        return dao.selectList(wrapper);
    }

}
